package cn.edu.gcu.giftems.action;

import cn.edu.gcu.giftems.pojos.User;
import cn.edu.gcu.giftems.service.UserService;
import cn.edu.gcu.giftems.utils.AuthCodeUtils;
import cn.edu.gcu.giftems.utils.export.UesrExportUtils;
import cn.edu.gcu.giftems.utils.PageUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;

import static cn.edu.gcu.giftems.utils.AuthCodeUtils.SOURCES;

/**
 * TODO: Description here...
 *
 * @author ZhengHQ
 * @create 2018-09-25
 * @see
 */
public class UserAction extends ActionSupport implements ModelDriven<User> {

    private UserService userService =
            (UserService) new ClassPathXmlApplicationContext("applicationContext.xml")
                    .getBean("userService");
    private User user = new User();
    private List<User> list;

    @Override
    public User getModel() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public List<User> getList() {
        return list;
    }

    public void setList(List<User> list) {
        this.list = list;
    }

    public String login(){
        //调用业务层的类
        User existUser = userService.login(user);
        if(existUser == null){
            //登录失败
            ActionContext.getContext().getSession().put("msg", "您输入的用户名或密码不正确，请再试一次。");
            return "login";
        }else{
            //登录成功
            ActionContext.getContext().getSession().put("username", existUser.getName());
            return "index";
        }
    }

    /**
     * 用户注册的执行方法
     */
    public String register(){
        String inputAuthCode = ActionContext.getContext().getParameters().get("code").toString().toLowerCase();
        String authCode = ActionContext.getContext().getSession().get("authCode").toString().toLowerCase();
        if (inputAuthCode.equals(authCode)) {
            userService.save(user);
            ActionContext.getContext().getSession().put("msg", "注册成功，请登录。");
            return "login";
        }
        ActionContext.getContext().getSession().put("codeMsg", "注册失败，验证码错误。");
        return "register";
    }

    /**
     * 分页查询用户的执行方法
     */
    private Integer currPage = 1;
    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }
    public String findByPage(){
        PageUtils<User> pageUtils = userService.findByPage(currPage);
        // ActionContext.getContext().getValueStack().push(pageUtils);
        pageUtils.setCurrPage(currPage);
        ActionContext.getContext().put("pageUtils", pageUtils);
        //list = pageUtils.getList();
        return "success";
    }
    /**
     * 添加用户的执行方法
     */
    public String save(){
        userService.save(user);
        return this.findByPage();
    }
    /**
     * 更新用户信息的执行方法
     */
    public String update(){
        if (user.getId() > 0)
            userService.update(user);
        return this.findByPage();
    }
    /**
     * 根据用户id删除用户
     */
    public String delete(){
        user = userService.findById(user.getId());
        if (user != null)
            userService.delete(user);
        return this.findByPage();
    }

    public String findPassword(){
        //调用业务层的类
        User existUser = userService.findPassword(user);
        if(existUser == null){
            //登录失败
            ActionContext.getContext().getSession().put("msg1", "您输入的用户名或手机号不正确，请再试一次。");
            return "findPassword";
        }else{
            //登录成功
            ActionContext.getContext().getSession().put("msg", "您的密码为："+existUser.getPwd());
            return "login";
        }
    }

    public String logout(){
        //注销成功
        ActionContext.getContext().getSession().put("username", null);
        ActionContext.getContext().getSession().put("msg", "您已注销");
        return "login";
    }

    public void export() {
        UesrExportUtils Export=new UesrExportUtils();
        try {
            Export.export(userService);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("导出数据有误");
        }
    }

    public String findByParameter() {
        String filter_name = ActionContext.getContext().getParameters().get("filter-name").toString();
        String simple_filter_value = ActionContext.getContext().getParameters().get("simple-filter-value").toString();
        PageUtils<User> pageUtils = userService.findByParameter(currPage, filter_name,simple_filter_value);
        ActionContext.getContext().put("pageUtils", pageUtils);
        return "success";
    }

    public String addAuthCode() {
        AuthCodeUtils authCodeUtils = new AuthCodeUtils();
        String authCode = authCodeUtils.generateString(new Random(), SOURCES, 6);
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.setAttribute("authCodeTip", "您的验证码为：");
        session.setAttribute("authCode", authCode);
        //session.setMaxInactiveInterval(20);
        return "index";
    }
}
